<?
require_once('./application/classes/item/bericht.class.php');

class berichtModel extends db implements db_interface{

private static $instance;

public function __construct($iDb){
	
		parent::__construct($iDb);
	
	}
	
public static function getInstance($iDb){
	
		if(self::$instance == NULL){
			self::$instance = new self($iDb);
		}
		
		return self::$instance;
	
	
	}
	
	
	public function getAantalBerichten(user_bewonersgroep $iUser){
		
		$query = $this -> db -> prepare("SELECT COUNT(*) FROM item i WHERE i.item_geb_id = ? AND i.item_type = ? AND i.item_deleted = 1");
		$bindVars = array($iUser -> id, 'bericht');
		$this -> bindVars($query, $bindVars);
		$query -> execute();
		$query -> bind_result($count);
		$query -> fetch();
		
		return $count;
	
	}
	

	
	
	public function getBerichten(user_bewonersgroep $iUser, $iStart, $iEnd){
	
		$query = $this -> db -> prepare('SELECT i.item_id,g.geb_naam,i.item_title,b.bericht_text,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join bericht b on (i.item_id = b.bericht_id) WHERE geb_id = ? AND i.item_deleted = 1 ORDER BY i.item_id DESC LIMIT ?, ?');
		
		$bindValue = $iUser -> id;
		
		$this -> bindVars($query,array($bindValue, $iStart, $iEnd));
		$query -> execute();
		
		$query -> bind_result($id,$geb_naam,$title,$text,$date); 
		
		$returnArray = array();
		
		while ($query -> fetch())
		{
			$bericht = new item_bericht($this -> db);
			$bericht -> id = $id;
			$bericht -> title = $title;
			$bericht -> geb_naam = $geb_naam;
			$bericht -> bericht_text = $text;
			$bericht -> geb_id = $iUser -> id;
			$returnArray[] = $bericht;
			$bericht -> datum = $date;
			
		}
		
		return $returnArray;
	
	
	}
	
	
	
		public function getHelpBerichten(){
	
		$query = $this -> db -> prepare('SELECT i.item_id,g.geb_naam,i.item_title,b.bericht_text,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join bericht b on (i.item_id = b.bericht_id) 
		WHERE i.item_deleted = 1 AND i.item_type = "help" ORDER BY i.item_id');
		
		
		
		
		$query -> execute();
		
		$query -> bind_result($id,$geb_naam,$title,$text,$date); 
		
		$returnArray = array();
		
		while ($query -> fetch())
		{
			$bericht = new item_bericht($this -> db);
			$bericht -> id = $id;
			$bericht -> title = $title;
			$bericht -> geb_naam = $geb_naam;
			$bericht -> bericht_text = $text;
			
			$returnArray[] = $bericht;
			$bericht -> datum = $date;
			
		}
		
		return $returnArray;
	
	
	}

	
	
	
	public function getAllBerichten(user_bewonersgroep $iUser){
	
		$query = $this -> db -> prepare('SELECT i.item_id,g.geb_naam,i.item_title,b.bericht_text,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join bericht b on (i.item_id = b.bericht_id) WHERE geb_id = ? AND i.item_deleted = 1 ORDER BY i.item_id DESC');
		
		$bindValue = $iUser -> id;
		
		$this -> bindVars($query,array($bindValue));
		$query -> execute();
		
		$query -> bind_result($id,$geb_naam,$title,$text,$date); 
		
		$returnArray = array();
		
		while ($query -> fetch())
		{
			$bericht = new item_bericht($this -> db);
			$bericht -> id = $id;
			$bericht -> title = $title;
			$bericht -> geb_naam = $geb_naam;
			$bericht -> bericht_text = $text;
			$bericht -> geb_id = $iUser -> id;
			$returnArray[] = $bericht;
			$bericht -> datum = $date;
			
		}
		
		return $returnArray;
	
	
	}
	
	public function addBericht(item_bericht $bericht){
		$query = $this -> db -> prepare("CALL berichtAdd(?, ?, ?,?)");	
		$bindingvars=array($bericht -> bericht_text , $bericht -> title , $bericht -> geb_id , 'bericht');
		$this -> bindVars($query,$bindingvars);
		$query -> execute();
		
	}
	
	public function addHelp(item_bericht $bericht){
		$query = $this -> db -> prepare("CALL berichtAdd(?, ?, ?,?)");	
		$bindingvars=array($bericht -> bericht_text , $bericht -> title , $bericht -> geb_id , 'help');
		$this -> bindVars($query,$bindingvars);
		$query -> execute();
		
	}
	
	public function delBericht(item_bericht $bericht)
	{
		
		$query = $this -> db -> prepare('call itemDel(?);');
		$bindValue = $bericht -> id;
		$query -> bind_param('i', $bindValue);
		$query -> execute();
	}
	public function updBericht(item_bericht $bericht)
	{
		$query = $this -> db -> prepare('call berichtUpd(?,?,?);');
		$bindingVars = array($bericht -> title , $bericht -> bericht_text , $bericht -> id);
		$this -> bindVars($query,$bindingVars);
		$query -> execute();
	}
	
	
	public function fill()
	{
		$query = $this -> db -> query('SELECT i.item_id,bg.bew_titel,g.geb_id,i.item_title,b.bericht_text,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as date FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join bewonersgroep bg on (bg.bew_id = g.geb_id) join bericht b on (i.item_id = b.bericht_id) where i.item_deleted = 1 ORDER BY i.item_id DESC LIMIT 5');
		
		
		
		$berichten = array();
		while ($row = $query->fetch_assoc())
		{
			$bericht = new item_bericht($this -> db);
			$bericht -> id = $row['item_id'];
			$bericht -> title = $row['item_title'];
			$bericht -> geb_naam = $row['bew_titel'];
			$bericht -> bericht_text = $row['bericht_text'];
			$bericht -> geb_id = $row['geb_id'];
			$bericht -> datum = $row['date'];
			$berichten[]= $bericht;
		}
		return $berichten;
	}
	
	public function getBerichtId(item_bericht $bericht)
	{
		$query = $this -> db -> prepare('SELECT i.item_id,bg.bew_titel,bg.bew_id,i.item_title,b.bericht_text,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as date FROM item i join bewonersgroep bg on (i.item_geb_id = bg.bew_id) join bericht b on (i.item_id = b.bericht_id) WHERE i.item_id = ?');
		
		$bindValue = $bericht -> id;
		$query -> bind_param('i', $bindValue);
		$query -> execute();
		
		$query -> bind_result($id,$geb_naam,$geb_id,$title,$text,$date); 
		
		$returnArray = array();
		
		$query -> fetch();
		
			
			$bericht -> id = $id;
			$bericht -> geb_id = $geb_id;
			$bericht -> title = $title;
			$bericht -> geb_naam = $geb_naam;
			$bericht -> bericht_text = $text;
			$bericht -> datum = $date;
			
			
		
		

	}
	
	
		public function getHelpId(item_bericht $bericht)
	{
		$query = $this -> db -> prepare('SELECT i.item_id,i.item_title,b.bericht_text,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as date FROM item i join bericht b on (i.item_id = b.bericht_id) WHERE i.item_id = ?');
		
		$bindValue = $bericht -> id;
		$query -> bind_param('i', $bindValue);
		$query -> execute();
		
		$query -> bind_result($id,$title,$text,$date); 
		
		$returnArray = array();
		
		$query -> fetch();
		
			
			$bericht -> id = $id;
		
			$bericht -> title = $title;
			
			$bericht -> bericht_text = $text;
			$bericht -> datum = $date;
			
			
		
		
		
		
	}
	
	

}



?>